import {
  authUserAssetLoadUrl,
  authUserAuthorizeUrl,
  authUserLoginUrl,
  authUserLogoutUrl,
  authUserTokenUrl,
  getAuthUserInfoUrl,
  getCaptchaCodeUrl,
  updatePasswordUrl,
  loginByCodeUrl,
  getUserSessionByIdUrl,
  kickoffUserUrl,
  KickoffUserSessionUrl
} from '@/apis/sop/auth-url'
import { bsRequest, request, ssoRequest } from '@/apis/request'

export const userLogin = async (params: any, codeInfo: any) => request(
  {
    url: authUserLoginUrl(''),
    method: 'post',
    data: params,
    headers: {
      captcha_key: codeInfo.key,
      captcha_code: codeInfo.code
    }
  })

/**
 * 下面是单点登录接口
 * @param params
 * @param codeInfo
 */
export const userAuthorize = async (params: any, codeInfo: any) => request({
  url: authUserAuthorizeUrl(''),
  method: 'post',
  data: params,
  headers: {
    captcha_key: codeInfo.key,
    captcha_code: codeInfo.code
  }
})

/**
 * 动态路由接口
 */
export const loadAuthUserAsset = async () => request(
  {
    url: authUserAssetLoadUrl(''),
    method: 'post'
  })

export function loginByCode(code: string) {
  return request({
    url: loginByCodeUrl('', code),
    method: 'post'
  })
}

export const getAuthUserInfo = async (accessToken: string) => bsRequest({
  url: getAuthUserInfoUrl(''),
  method: 'post',
  headers: {
    access_token: accessToken
  },
  data: {}
})
export const userToken = async (code: any) => request(
  { url: authUserTokenUrl('') + `?code=${code}`, method: 'post', data: {} })

export const userLogout = async () => request(
  { url: authUserLogoutUrl(''), method: 'post', data: {} })

export const updatePassword = async (user: any) => request(
  { url: updatePasswordUrl(''), method: 'post', data: user })

export const getCaptchaCode = async () => ssoRequest(
  { url: getCaptchaCodeUrl(''), method: 'post' })

export const getUserSessionById = async (params: any) => request(
  { url: getUserSessionByIdUrl(''), method: 'post', data: params })

export const KickoffUser = async (accessToken: string, realmId: string) => request(
  {
    url: kickoffUserUrl('') + '?accessToken=' + accessToken + '&realmId=' + realmId,
    method: 'post', data: {}
  })
export const KickoffUserSession = async (loginId: string) => request(
  { url: KickoffUserSessionUrl('') + '?loginId=' + loginId, method: 'post', data: {} })
